<template>
  <div class="con">
    <el-breadcrumb separator=">">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <button
      type="button"
      class="el-button el-button--primary is-plain bu"
      @click="addObj.addFnRolesDialog = true"
    >
      <span>添加角色</span>
    </button>
    <el-table :data="tableData.rolesList" height="678" border>
      <el-table-column type="expand" width="50">
        <template #default="scope">
          <el-row v-for="item1 in scope.row.children" :key="item1.id">
            <el-col :span="4">
              <el-tag closable @close="deleteRolesRightFn(scope.row, item1.id)">
                {{ item1.authName }}
              </el-tag>
            </el-col>
            <el-col :span="20">
              <el-row v-for="item2 in item1.children" :key="item2.id">
                <el-col :span="6">
                  <el-tag
                    closable
                    type="success"
                    @close="deleteRolesRightFn(scope.row, item2.id)"
                  >
                    {{ item2.authName }}
                  </el-tag>
                </el-col>
                <el-col :span="18">
                  <el-tag
                    closable
                    type="warning"
                    v-for="item3 in item2.children"
                    :key="item3.id"
                    @close="deleteRolesRightFn(scope.row, item3.id)"
                  >
                    {{ item3.authName }}
                  </el-tag>
                </el-col>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-table-column>
      <el-table-column type="index" label="ID" width="50"> </el-table-column>
      <el-table-column prop="roleName" label="角色名称" width="180">
      </el-table-column>
      <el-table-column prop="roleDesc" label="角色描述" width="180">
      </el-table-column>
      <el-table-column prop="level" label="操作">
        <template #default="scope">
          <button
            type="button"
            class="el-button el-button--success el-button--small is-circle"
            @click="editRolesDialog(scope.row)"
          >
            <i class="el-icon-edit"></i>
          </button>
          <button
            type="button"
            class="el-button el-button--info el-button--small is-circle"
            @click="deleteRolesFn(scope.row.id)"
          >
            <i class="el-icon-delete"></i>
          </button>
          <button
            type="button"
            class="el-button el-button--warning el-button--small is-circle"
            @click="getTreeFn(scope.row)"
          >
            <i class="el-icon-check"></i>
          </button>
        </template>
      </el-table-column>
    </el-table>
    <el-dialog title="添加角色" v-model="addObj.addFnRolesDialog">
      <el-form :model="addObj.addFnRolesForm" ref="addFnRolesForm">
        <el-form-item
          label="角色名"
          :label-width="addObj.formLabelWidth"
          prop="roleName"
          :rules="[
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="addObj.addFnRolesForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="addObj.formLabelWidth"
          prop="roleDesc"
          :rules="[
            { required: true, message: '请输入角色描述', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="addObj.addFnRolesForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="addObj.addFnRolesDialog = false">取 消</el-button>
        <el-button type="primary" @click="addRolesFn()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog title="编辑角色" v-model="editObj.editFnRolesDialog">
      <el-form :model="editObj.editFnRolesForm" ref="editFnRolesForm">
        <el-form-item label="角色id" :label-width="editObj.formLabelWidth">
          <el-input
            v-model="editObj.editFnRolesForm.id"
            autocomplete="off"
            disabled
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色名"
          :label-width="editObj.formLabelWidth"
          prop="roleName"
          :rules="[
            { required: true, message: '请输入用户名', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="editObj.editFnRolesForm.roleName"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item
          label="角色描述"
          :label-width="editObj.formLabelWidth"
          prop="roleDesc"
          :rules="[
            { required: true, message: '请输入角色描述', trigger: 'blur' },
          ]"
        >
          <el-input
            v-model="editObj.editFnRolesForm.roleDesc"
            autocomplete="off"
          ></el-input>
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button @click="editObj.editFnRolesDialog = false">取 消</el-button>
        <el-button type="primary" @click="editRoles()">确 定</el-button>
      </div>
    </el-dialog>
    <el-dialog
      title="权限分配"
      v-model="editTree.editRloesTreeDialog"
      @close="handleClose"
    >
      <el-tree
        :data="editTree.treeData"
        show-checkbox
        node-key="id"
        ref="tree"
        :default-expanded-keys="editTree.efaultExpandedKeys"
        :default-checked-keys="editTree.efaultExpandedKeys"
        :props="editTree.defaultProps"
      >
      </el-tree>

      <div class="dialog-footer">
        <el-button @click="editTree.editRloesTreeDialog = false"
          >取 消</el-button
        >
        <el-button type="primary" @click="editRolesRightsId">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {
  getRoles,
  addRoles,
  deleteRoles,
  editRolesById,
  deleteRolesRight,
  getTree,
  assignRoles,
  rolesAuthorize,
} from "../../http/api";
import { onMounted, reactive, getCurrentInstance, nextTick } from "vue";
export default {
  setup() {
    let { ctx } = getCurrentInstance();
    let tableData = reactive({
      rolesList: [],
    });
    let addObj = reactive({
      addFnRolesDialog: false,
      formLabelWidth: "80px",
      addFnRolesForm: {
        roleName: "",
        roleDesc: "",
      },
    });
    let editObj = reactive({
      formLabelWidth: "80px",
      editFnRolesDialog: false,
      editFnRolesForm: {
        id: "",
        roleName: "",
        roleDesc: "",
      },
    });
    let editTree = reactive({
      editRloesTreeDialog: false,
      treeData: [],
      defaultProps: {
        children: "children",
        label: "authName",
      },
      defaultExpandedKeys: [],
      roleId: "",
      arr: [],
    });
    //角色列表
    const getRolesFn = async () => {
      let res = await getRoles();
      tableData.rolesList = res;
      //   console.log(tableData.rolesList[0]);
    };
    //添加角色
    const addRolesFn = async () => {
      await addRoles(addObj.addFnRolesForm);
      addObj.addFnRolesDialog = false;
      getRolesFn();
    };
    //删除角色
    const deleteRolesFn = async (id) => {
      await deleteRoles(id);
      getRolesFn();
    };
    //编辑角色 回显信息
    const editRolesDialog = (obj) => {
      console.log("obj", obj);
      editObj.editFnRolesDialog = true;
      editObj.editFnRolesForm = { ...obj };
    };
    //编辑提交角色
    const editRoles = async () => {
      const { id, roleName, roleDesc } = editObj.editFnRolesForm;
      await editRolesById(id, { roleName, roleDesc });
      editObj.editFnRolesDialog = false;
      getRolesFn();
    };
    //删除角色指定权限
    const deleteRolesRightFn = async (scopeRow, rightId) => {
      const res = await deleteRolesRight(scopeRow.id, rightId);
      scopeRow.children = res;
    };
    //获取权限列表
    const getTreeFn = async (obj) => {
      editTree.editRloesTreeDialog = true;
      const res = await getTree();
      editTree.treeData = res;
      console.log(obj);
      editTree.roleId = obj.id;
      console.log("obj", obj);
      await nextTick(() => {
        editTree.arr = [];
        deepArr(obj.children);
        ctx.$refs.tree.setCheckedNodes(editTree.arr);
        editTree.defaultExpandedKeys = ctx.$refs.tree.getCheckedKeys();
      });
      console.log(editTree);
    };
    const deepArr = (array) => {
      for (let i in array) {
        let data = array[i];
        if (data.children) {
          deepArr(data.children);
        } else {
          editTree.arr.push(data);
        }
      }
    };
    const handleClose = () => {
      editTree.defaultExpandedKeys = [];
      editTree.treeData = [];
    };
    //角色授权
    const editRolesRightsId = async () => {
      console.log(editTree);
      console.log("---->", ctx.$refs.tree.getCheckedKeys());
      let arr = [];
      let nodes = ctx.$refs.tree.getCheckedNodes(false, true);
      nodes.map((item) => {
        arr.push(item.id);
      });
      arr=arr.join(',')
      console.log(editTree.roleId, arr);
      await rolesAuthorize(editTree.roleId,{rids:arr});
      editTree.editRloesTreeDialog = false;
      getRolesFn();
    };
    onMounted(() => {
      getRolesFn();
    });
    return {
      tableData,
      getRolesFn,
      addObj,
      addRolesFn,
      deleteRolesFn,
      editObj,
      editRolesDialog,
      editRoles,
      deleteRolesRightFn,
      editTree,
      getTreeFn,
      deepArr,
      handleClose,
      editRolesRightsId,
    };
  },
};
</script>
<style scoped>
.el-breadcrumb {
  padding-top: 10px;
  margin-bottom: 10px;
  margin-left: 10px;
}
.bu {
  margin-left: 10px;
}
.con {
  width: 98%;
  height: auto;
  background: #fff;
  margin: 10px auto;
  overflow: hidden;
}
.el-table {
  width: 99%;
  margin: 10px auto;
}
.el-tag {
  margin-top: 5px;
  margin-left: 5px;
}
</style>